<?php

namespace app\modules\Order\models;

use Yii;
use app\modules\Order\models\OrderPrice;
/**
 * This is the model class for table "order_price_type".
 *
 * @property integer $id
 * @property integer $product_id
 * @property string $name
 * @property string $desc
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class OrderPriceType extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_price_type';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product_id', 'status', 'user_id', 'type'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['price'], 'number'],
            [['name'], 'string', 'max' => 128],
            [['desc'], 'string', 'max' => 255],
        ];
    }

    public function getOrderPrice(){
        $orderPrice = OrderPrice::find()->joinWith('order')->andWhere(['order_price.type'=>$this->id,'order_price.status'=>1])->andWhere(['<=','order.status',4])->orderBy('order.status asc')->all();
        return $orderPrice;
        
    }

    // 附加费 订单人数
    public function getCustomerNum(){

        $orderPrice = OrderPrice::find()->where(['type'=>$this->id,'status'=>1])->all();

        $nums = ['total'=>0, 1=>0, 2=>0,3=>0, 4=>0];
        foreach($orderPrice as $o){
            if($o->order->status == 1){ //占位
                $nums[1] += $o->num;  
            }else if($o->order->status == 2){ //付定金
                $nums[2] += $o->num; 
            }else if($o->order->status == 3){ //已付款
                $nums[3] += $o->num;  
            }else if($o->order->status == 4){ //确认提交
                $nums[4] += $o->num;  
            }
        }
        $nums['total'] = $nums[1]+$nums[2]+$nums[3]+$nums[4];
        return $nums;

    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app', 'Product ID'),
            'name' => Yii::t('app', 'Name'),
            'desc' => Yii::t('app', 'Desc'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'price' => Yii::t('app', 'Price'),
            'type' => Yii::t('app', '附加费类型'),
        ];
    }


    // 保存前处理
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
            $this->status 	  = 1;
        }
        return parent::beforeSave($insert);
    }//}}}


 	// 保存后执行
	public function afterSave($insert, $changedAttributes){//{{{
		parent::afterSave($insert, $changedAttributes);

		if($insert) {
			// 日志
			$desc = sprintf("创建附加费  %s", $this->name);
			Yii::$app->logs->info($this->product_id, 1, $desc, '' , 2);
		}
	}//}}}





}
